//
//  ViewController.m
//  封装下载
//
//  Created by dllo on 16/3/7.
//  Copyright © 2016年 巨兔. All rights reserved.
//

#import "ViewController.h"
#import "DownLoad.h"
#import "DownLoadViewController.h"

@interface ViewController ()<DownloadDelegate>

@property (weak, nonatomic) IBOutlet UIProgressView *progress;
@property (weak, nonatomic) IBOutlet UILabel *total; //已下载/共
@property (weak, nonatomic) IBOutlet UILabel *now; //下载速度
@property (weak, nonatomic) IBOutlet UIButton *start; //开始/暂停
@property (nonatomic, retain) DownLoad *download;

@end

@implementation ViewController

//http://hc25.aipai.com/user/656/20448656/6167672/card/25033081/card.mp4?l=a

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.download = [[DownLoad alloc] initWithURL:@"http://img5.duitang.com/uploads/blog/201408/01/20140801151328_RrurB.jpeg"];
    _download.delegate = self;
    [_start setTitle:@"开始" forState:UIControlStateNormal];
    [_start setTitle:@"暂停" forState:UIControlStateSelected];
   
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 50, 100, 50);
    [self.view addSubview:button];
    button.backgroundColor = [UIColor redColor];
    [button setTitle:@"查看下载" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)buttonClick
{
    DownLoadViewController *down = [[DownLoadViewController alloc] init];
    [self presentViewController:down animated:YES completion:^{
        
    }];
}

- (void)getWithspeed:(float)speed done:(float)done total:(float)total
{
    self.total.text = [NSString stringWithFormat:@"%.2fMB/%.2fMB", done, total];
    self.now.text = [NSString stringWithFormat:@"%.2fKB/s", speed];
    /* 进度条设置 */
    float progress = done * 1.0f / total;
    [self.progress setProgress:progress];
}

- (IBAction)start:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        [_download start];
    } else {
        [_download suspend];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
